/*
 * @(#)FormPage.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.form.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import com.dcivision.form.bean.FormElement;
import com.dcivision.form.bean.FormPage;
import com.dcivision.form.bean.FormSection;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.workflow.bean.WorkflowStep;
import com.dcivision.workflow.dao.MtmWfStepFormRecordDAObject;

/**
  FormPageDAObject.java

  This class is the data access bean for table "FORM_PAGE".

  @author      Scott Tong
  @company     DCIVision Limited
  @creation date   23/12/2003
  @version     $Revision: 1.21.4.1 $
*/

public class FormPageDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.21.4.1 $";

  public static final String TABLE_NAME = "FORM_PAGE";

  public FormPageDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("FORM_RECORD_ID");
    this.vecDBColumn.add("PARENT_PAGE_ID");
    this.vecDBColumn.add("TITLE");
    this.vecDBColumn.add("TITLE_ALT");
    this.vecDBColumn.add("PAGE_NO");
    this.vecDBColumn.add("PAGE_CODE");
    this.vecDBColumn.add("DESCRIPTION");
    this.vecDBColumn.add("DESCRIPTION_ALT");
    this.vecDBColumn.add("TITLE_FONT_COLOR");
    this.vecDBColumn.add("TITLE_FONT_SIZE");
    this.vecDBColumn.add("TITLE_FONT_TYPE");
    this.vecDBColumn.add("TITLE_FONT_STYLE");
    this.vecDBColumn.add("DESC_FONT_COLOR");
    this.vecDBColumn.add("DESC_FONT_SIZE");
    this.vecDBColumn.add("DESC_FONT_TYPE");
    this.vecDBColumn.add("DESC_FONT_STYLE");
    this.vecDBColumn.add("GROUP_ID");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.FORM_RECORD_ID, A.PARENT_PAGE_ID, A.TITLE, A.TITLE_ALT, A.PAGE_NO, A.PAGE_CODE, A.DESCRIPTION, A.DESCRIPTION_ALT, A.TITLE_FONT_COLOR, A.TITLE_FONT_SIZE, A.TITLE_FONT_TYPE, A.TITLE_FONT_STYLE, A.DESC_FONT_COLOR, A.DESC_FONT_SIZE, A.DESC_FONT_TYPE, A.DESC_FONT_STYLE, A.GROUP_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ,A.TITLE_POSITION ");
        sqlStat.append("FROM   FORM_PAGE A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          FormPage tmpFormPage = new FormPage();
          tmpFormPage.setID(getResultSetInteger(rs, "ID"));
          tmpFormPage.setFormRecordID(getResultSetInteger(rs, "FORM_RECORD_ID"));
          tmpFormPage.setParentPageID(getResultSetInteger(rs, "PARENT_PAGE_ID"));
          tmpFormPage.setTitle(getResultSetString(rs, "TITLE"));
          tmpFormPage.setTitleAlt(getResultSetString(rs, "TITLE_ALT"));
          tmpFormPage.setPageNo(getResultSetInteger(rs, "PAGE_NO"));
          tmpFormPage.setPageCode(getResultSetString(rs, "PAGE_CODE"));
          tmpFormPage.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpFormPage.setDescriptionAlt(getResultSetString(rs, "DESCRIPTION_ALT"));
          tmpFormPage.setTitleFontColor(getResultSetString(rs, "TITLE_FONT_COLOR"));
          tmpFormPage.setTitleFontSize(getResultSetInteger(rs, "TITLE_FONT_SIZE"));
          tmpFormPage.setTitleFontType(getResultSetString(rs, "TITLE_FONT_TYPE"));
          tmpFormPage.setTitleFontStyle(getResultSetString(rs, "TITLE_FONT_STYLE"));
          tmpFormPage.setDescFontColor(getResultSetString(rs, "DESC_FONT_COLOR"));
          tmpFormPage.setDescFontSize(getResultSetInteger(rs, "DESC_FONT_SIZE"));
          tmpFormPage.setDescFontType(getResultSetString(rs, "DESC_FONT_TYPE"));
          tmpFormPage.setDescFontStyle(getResultSetString(rs, "DESC_FONT_STYLE"));
          tmpFormPage.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
          tmpFormPage.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormPage.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormPage.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormPage.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormPage.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormPage.settitlePosition(getResultSetString(rs, "TITLE_POSITION"));
          tmpFormPage.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormPage.setCreatorName(UserInfoFactory.getUserFullName(tmpFormPage.getCreatorID()));
          tmpFormPage.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormPage.getUpdaterID()));
          return(tmpFormPage);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized AbstractBaseObject getFirstPageByFormRecordID(Integer formRecordID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.FORM_RECORD_ID, A.PARENT_PAGE_ID, A.TITLE, A.TITLE_ALT, A.PAGE_NO, A.PAGE_CODE, A.DESCRIPTION, A.DESCRIPTION_ALT, A.TITLE_FONT_COLOR, A.TITLE_FONT_SIZE, A.TITLE_FONT_TYPE, A.TITLE_FONT_STYLE, A.DESC_FONT_COLOR, A.DESC_FONT_SIZE, A.DESC_FONT_TYPE, A.DESC_FONT_STYLE, A.GROUP_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE , A.TITLE_POSITION ");
        sqlStat.append("FROM   FORM_PAGE A ");
        sqlStat.append("WHERE  A.FORM_RECORD_ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, formRecordID);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          FormPage tmpFormPage = new FormPage();
          tmpFormPage.setID(getResultSetInteger(rs, "ID"));
          tmpFormPage.setFormRecordID(getResultSetInteger(rs, "FORM_RECORD_ID"));
          tmpFormPage.setParentPageID(getResultSetInteger(rs, "PARENT_PAGE_ID"));
          tmpFormPage.setTitle(getResultSetString(rs, "TITLE"));
          tmpFormPage.setTitleAlt(getResultSetString(rs, "TITLE_ALT"));
          tmpFormPage.setPageNo(getResultSetInteger(rs, "PAGE_NO"));
          tmpFormPage.setPageCode(getResultSetString(rs, "PAGE_CODE"));
          tmpFormPage.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpFormPage.setDescriptionAlt(getResultSetString(rs, "DESCRIPTION_ALT"));
          tmpFormPage.setTitleFontColor(getResultSetString(rs, "TITLE_FONT_COLOR"));
          tmpFormPage.setTitleFontSize(getResultSetInteger(rs, "TITLE_FONT_SIZE"));
          tmpFormPage.setTitleFontType(getResultSetString(rs, "TITLE_FONT_TYPE"));
          tmpFormPage.setTitleFontStyle(getResultSetString(rs, "TITLE_FONT_STYLE"));
          tmpFormPage.setDescFontColor(getResultSetString(rs, "DESC_FONT_COLOR"));
          tmpFormPage.setDescFontSize(getResultSetInteger(rs, "DESC_FONT_SIZE"));
          tmpFormPage.setDescFontType(getResultSetString(rs, "DESC_FONT_TYPE"));
          tmpFormPage.setDescFontStyle(getResultSetString(rs, "DESC_FONT_STYLE"));
          tmpFormPage.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
          tmpFormPage.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormPage.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormPage.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormPage.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormPage.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormPage.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormPage.settitlePosition(getResultSetString(rs, "TITLE_POSITION"));
          tmpFormPage.setCreatorName(UserInfoFactory.getUserFullName(tmpFormPage.getCreatorID()));
          tmpFormPage.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormPage.getUpdaterID()));

          return(tmpFormPage);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.FORM_RECORD_ID, A.PARENT_PAGE_ID, A.TITLE, A.TITLE_ALT, A.PAGE_NO, A.PAGE_CODE, A.DESCRIPTION, A.DESCRIPTION_ALT, A.TITLE_FONT_COLOR, A.TITLE_FONT_SIZE, A.TITLE_FONT_TYPE, A.TITLE_FONT_STYLE, A.DESC_FONT_COLOR, A.DESC_FONT_SIZE, A.DESC_FONT_TYPE, A.DESC_FONT_STYLE, A.GROUP_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE,A.TITLE_POSITION ");
        sqlStat.append("FROM   FORM_PAGE A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          FormPage tmpFormPage = new FormPage();
          tmpFormPage.setID(getResultSetInteger(rs, "ID"));
          tmpFormPage.setFormRecordID(getResultSetInteger(rs, "FORM_RECORD_ID"));
          tmpFormPage.setParentPageID(getResultSetInteger(rs, "PARENT_PAGE_ID"));
          tmpFormPage.setTitle(getResultSetString(rs, "TITLE"));
          tmpFormPage.setTitleAlt(getResultSetString(rs, "TITLE_ALT"));
          tmpFormPage.setPageNo(getResultSetInteger(rs, "PAGE_NO"));
          tmpFormPage.setPageCode(getResultSetString(rs, "PAGE_CODE"));
          tmpFormPage.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpFormPage.setDescriptionAlt(getResultSetString(rs, "DESCRIPTION_ALT"));
          tmpFormPage.setTitleFontColor(getResultSetString(rs, "TITLE_FONT_COLOR"));
          tmpFormPage.setTitleFontSize(getResultSetInteger(rs, "TITLE_FONT_SIZE"));
          tmpFormPage.setTitleFontType(getResultSetString(rs, "TITLE_FONT_TYPE"));
          tmpFormPage.setTitleFontStyle(getResultSetString(rs, "TITLE_FONT_STYLE"));
          tmpFormPage.setDescFontColor(getResultSetString(rs, "DESC_FONT_COLOR"));
          tmpFormPage.setDescFontSize(getResultSetInteger(rs, "DESC_FONT_SIZE"));
          tmpFormPage.setDescFontType(getResultSetString(rs, "DESC_FONT_TYPE"));
          tmpFormPage.setDescFontStyle(getResultSetString(rs, "DESC_FONT_STYLE"));
          tmpFormPage.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
          tmpFormPage.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormPage.settitlePosition(getResultSetString(rs, "TITLE_POSITION"));
          tmpFormPage.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormPage.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormPage.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormPage.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormPage.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormPage.setCreatorName(UserInfoFactory.getUserFullName(tmpFormPage.getCreatorID()));
          tmpFormPage.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormPage.getUpdaterID()));
          tmpFormPage.setRecordCount(totalNumOfRecord);
          tmpFormPage.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpFormPage);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.FORM_RECORD_ID, A.PARENT_PAGE_ID, A.TITLE, A.TITLE_ALT, A.PAGE_NO, A.PAGE_CODE, A.DESCRIPTION, A.DESCRIPTION_ALT, A.TITLE_FONT_COLOR, A.TITLE_FONT_SIZE, A.TITLE_FONT_TYPE, A.TITLE_FONT_STYLE, A.DESC_FONT_COLOR, A.DESC_FONT_SIZE, A.DESC_FONT_TYPE, A.DESC_FONT_STYLE, A.GROUP_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ,A.TITLE_POSITION ");
        sqlStat.append("FROM   FORM_PAGE A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          FormPage tmpFormPage = new FormPage();
          tmpFormPage.setID(getResultSetInteger(rs, "ID"));
          tmpFormPage.setFormRecordID(getResultSetInteger(rs, "FORM_RECORD_ID"));
          tmpFormPage.setParentPageID(getResultSetInteger(rs, "PARENT_PAGE_ID"));
          tmpFormPage.setTitle(getResultSetString(rs, "TITLE"));
          tmpFormPage.setTitleAlt(getResultSetString(rs, "TITLE_ALT"));
          tmpFormPage.setPageNo(getResultSetInteger(rs, "PAGE_NO"));
          tmpFormPage.setPageCode(getResultSetString(rs, "PAGE_CODE"));
          tmpFormPage.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpFormPage.setDescriptionAlt(getResultSetString(rs, "DESCRIPTION_ALT"));
          tmpFormPage.setTitleFontColor(getResultSetString(rs, "TITLE_FONT_COLOR"));
          tmpFormPage.setTitleFontSize(getResultSetInteger(rs, "TITLE_FONT_SIZE"));
          tmpFormPage.setTitleFontType(getResultSetString(rs, "TITLE_FONT_TYPE"));
          tmpFormPage.setTitleFontStyle(getResultSetString(rs, "TITLE_FONT_STYLE"));
          tmpFormPage.setDescFontColor(getResultSetString(rs, "DESC_FONT_COLOR"));
          tmpFormPage.setDescFontSize(getResultSetInteger(rs, "DESC_FONT_SIZE"));
          tmpFormPage.setDescFontType(getResultSetString(rs, "DESC_FONT_TYPE"));
          tmpFormPage.setDescFontStyle(getResultSetString(rs, "DESC_FONT_STYLE"));
          tmpFormPage.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
          tmpFormPage.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormPage.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormPage.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormPage.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormPage.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormPage.settitlePosition(getResultSetString(rs, "TITLE_POSITION"));
          tmpFormPage.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormPage.setCreatorName(UserInfoFactory.getUserFullName(tmpFormPage.getCreatorID()));
          tmpFormPage.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormPage.getUpdaterID()));
          result.add(tmpFormPage);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    FormPage tmpFormPage = (FormPage)((FormPage)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   FORM_PAGE(ID, FORM_RECORD_ID, PARENT_PAGE_ID, TITLE, TITLE_ALT, PAGE_NO, PAGE_CODE, DESCRIPTION, DESCRIPTION_ALT, TITLE_FONT_COLOR, TITLE_FONT_SIZE, TITLE_FONT_TYPE, TITLE_FONT_STYLE, DESC_FONT_COLOR, DESC_FONT_SIZE, DESC_FONT_TYPE, DESC_FONT_STYLE, GROUP_ID, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE,TITLE_POSITION) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpFormPage.getFormRecordID());
        setPrepareStatement(preStat, 3, tmpFormPage.getParentPageID());
        setPrepareStatement(preStat, 4, tmpFormPage.getTitle());
        setPrepareStatement(preStat, 5, tmpFormPage.getTitleAlt());
        setPrepareStatement(preStat, 6, tmpFormPage.getPageNo());
        setPrepareStatement(preStat, 7, tmpFormPage.getPageCode());
        setPrepareStatement(preStat, 8, tmpFormPage.getDescription());
        setPrepareStatement(preStat, 9, tmpFormPage.getDescriptionAlt());
        setPrepareStatement(preStat, 10, tmpFormPage.getTitleFontColor());
        setPrepareStatement(preStat, 11, tmpFormPage.getTitleFontSize());
        setPrepareStatement(preStat, 12, tmpFormPage.getTitleFontType());
        setPrepareStatement(preStat, 13, tmpFormPage.getTitleFontStyle());
        setPrepareStatement(preStat, 14, tmpFormPage.getDescFontColor());
        setPrepareStatement(preStat, 15, tmpFormPage.getDescFontSize());
        setPrepareStatement(preStat, 16, tmpFormPage.getDescFontType());
        setPrepareStatement(preStat, 17, tmpFormPage.getDescFontStyle());
        setPrepareStatement(preStat, 18, tmpFormPage.getGroupID());
        setPrepareStatement(preStat, 19, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 20, new Integer(0));
        setPrepareStatement(preStat, 21, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 22, currTime);
        setPrepareStatement(preStat, 23, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 24, currTime);
        setPrepareStatement(preStat, 25, tmpFormPage.gettitlePosition());
        preStat.executeUpdate();
        tmpFormPage.setID(nextID);
        tmpFormPage.setCreatorID(sessionContainer.getUserRecordID());
        tmpFormPage.setCreateDate(currTime);
        tmpFormPage.setUpdaterID(sessionContainer.getUserRecordID());
        tmpFormPage.setUpdateDate(currTime);
        tmpFormPage.setUpdateCount(new Integer(0));
        tmpFormPage.setCreatorName(UserInfoFactory.getUserFullName(tmpFormPage.getCreatorID()));
        tmpFormPage.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormPage.getUpdaterID()));
        return(tmpFormPage);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }


    protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
      PreparedStatement preStat = null;
      StringBuffer sqlStat = new StringBuffer();
      FormPage tmpFormPage = (FormPage)((FormPage)obj).clone();

      synchronized(dbConn) {
        try {
          int updateCnt = 0;
          Timestamp currTime = Utility.getCurrentTimestamp();
          sqlStat.append("UPDATE FORM_PAGE ");
          sqlStat.append("SET  FORM_RECORD_ID=?, PARENT_PAGE_ID=?, TITLE=?, TITLE_ALT=?, PAGE_NO=?, PAGE_CODE=?, DESCRIPTION=?, DESCRIPTION_ALT=?, TITLE_FONT_COLOR=?, TITLE_FONT_SIZE=?, TITLE_FONT_TYPE=?, TITLE_FONT_STYLE=?, DESC_FONT_COLOR=?, DESC_FONT_SIZE=?, DESC_FONT_TYPE=?, DESC_FONT_STYLE=?, GROUP_ID=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=?, TITLE_POSITION=? ");
          sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          setPrepareStatement(preStat, 1, tmpFormPage.getFormRecordID());
          setPrepareStatement(preStat, 2, tmpFormPage.getParentPageID());
          setPrepareStatement(preStat, 3, tmpFormPage.getTitle());
          setPrepareStatement(preStat, 4, tmpFormPage.getTitleAlt());
          setPrepareStatement(preStat, 5, tmpFormPage.getPageNo());
          setPrepareStatement(preStat, 6, tmpFormPage.getPageCode());
          setPrepareStatement(preStat, 7, tmpFormPage.getDescription());
          setPrepareStatement(preStat, 8, tmpFormPage.getDescriptionAlt());
          setPrepareStatement(preStat, 9, tmpFormPage.getTitleFontColor());
          setPrepareStatement(preStat, 10, tmpFormPage.getTitleFontSize());
          setPrepareStatement(preStat, 11, tmpFormPage.getTitleFontType());
          setPrepareStatement(preStat, 12, tmpFormPage.getTitleFontStyle());
          setPrepareStatement(preStat, 13, tmpFormPage.getDescFontColor());
          setPrepareStatement(preStat, 14, tmpFormPage.getDescFontSize());
          setPrepareStatement(preStat, 15, tmpFormPage.getDescFontType());
          setPrepareStatement(preStat, 16, tmpFormPage.getDescFontStyle());
          setPrepareStatement(preStat, 17, tmpFormPage.getGroupID());
          setPrepareStatement(preStat, 18, new Integer(tmpFormPage.getUpdateCount().intValue() + 1));
          setPrepareStatement(preStat, 19, sessionContainer.getUserRecordID());
          setPrepareStatement(preStat, 20, currTime);
          setPrepareStatement(preStat, 21, tmpFormPage.gettitlePosition());
          setPrepareStatement(preStat, 22, tmpFormPage.getID());
          setPrepareStatement(preStat, 23, tmpFormPage.getUpdateCount());
          updateCnt = preStat.executeUpdate();
          if (updateCnt == 0) {
            throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
          } else {
            tmpFormPage.setUpdaterID(sessionContainer.getUserRecordID());
            tmpFormPage.setUpdateDate(currTime);
            tmpFormPage.setUpdateCount(new Integer(tmpFormPage.getUpdateCount().intValue() + 1));
            tmpFormPage.setCreatorName(UserInfoFactory.getUserFullName(tmpFormPage.getCreatorID()));
            tmpFormPage.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormPage.getUpdaterID()));
            return(tmpFormPage);
          }
        } catch (ApplicationException appEx) {
          throw appEx;
        } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
        } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
        } finally {
          try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        }
      }
    }


   public synchronized  void updateByFile(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    FormPage tmpFormPage = (FormPage)((FormPage)obj).clone();
    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE FORM_PAGE ");
        sqlStat.append("SET   TITLE='"+tmpFormPage.getTitle()+"',  ");

        if (tmpFormPage.getTitleFontColor()!=null)
         sqlStat.append(" TITLE_FONT_COLOR='"+tmpFormPage.getTitleFontColor()+"',   ");
        if (tmpFormPage.getTitleFontSize()!=null)
         sqlStat.append(" TITLE_FONT_SIZE="+tmpFormPage.getTitleFontSize()+",   ");
        if (tmpFormPage.getTitleFontType()!=null)
         sqlStat.append(" TITLE_FONT_TYPE='"+tmpFormPage.getTitleFontType()+"' ,   ");
        if (tmpFormPage.getTitleFontStyle()!=null)
         sqlStat.append(" TITLE_FONT_STYLE='"+tmpFormPage.getTitleFontStyle()+"',   ");

        if (tmpFormPage.gettitlePosition()!=null)
         sqlStat.append("  TITLE_POSITION='"+tmpFormPage.gettitlePosition()+"',   ");

         sqlStat.append(" UPDATE_COUNT=UPDATE_COUNT+1  WHERE  ID="+tmpFormPage.getID());

         preStat = dbConn.prepareStatement(sqlStat.toString());

         preStat.executeUpdate();
         dbConn.commit();

       } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    FormPage tmpFormPage = (FormPage)((FormPage)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   FORM_PAGE ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpFormPage.getID());
        setPrepareStatement(preStat, 2, tmpFormPage.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpFormPage);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    FormPage tmpFormPage = (FormPage)this.oldValue;

    if (tmpFormPage != null) {
      oldValues.add(toAuditTrailValue(tmpFormPage.getID()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getFormRecordID()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getParentPageID()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getTitle()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getTitleAlt()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getPageNo()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getPageCode()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getDescription()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getDescriptionAlt()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getTitleFontColor()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getTitleFontSize()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getTitleFontType()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getTitleFontStyle()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getDescFontColor()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getDescFontSize()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getDescFontType()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getDescFontStyle()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getGroupID()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpFormPage.getUpdateDate()));
    }

    tmpFormPage = (FormPage)obj;
    if (tmpFormPage != null) {
      newValues.add(toAuditTrailValue(tmpFormPage.getID()));
      newValues.add(toAuditTrailValue(tmpFormPage.getFormRecordID()));
      newValues.add(toAuditTrailValue(tmpFormPage.getParentPageID()));
      newValues.add(toAuditTrailValue(tmpFormPage.getTitle()));
      newValues.add(toAuditTrailValue(tmpFormPage.getTitleAlt()));
      newValues.add(toAuditTrailValue(tmpFormPage.getPageNo()));
      newValues.add(toAuditTrailValue(tmpFormPage.getPageCode()));
      newValues.add(toAuditTrailValue(tmpFormPage.getDescription()));
      newValues.add(toAuditTrailValue(tmpFormPage.getDescriptionAlt()));
      newValues.add(toAuditTrailValue(tmpFormPage.getTitleFontColor()));
      newValues.add(toAuditTrailValue(tmpFormPage.getTitleFontSize()));
      newValues.add(toAuditTrailValue(tmpFormPage.getTitleFontType()));
      newValues.add(toAuditTrailValue(tmpFormPage.getTitleFontStyle()));
      newValues.add(toAuditTrailValue(tmpFormPage.getDescFontColor()));
      newValues.add(toAuditTrailValue(tmpFormPage.getDescFontSize()));
      newValues.add(toAuditTrailValue(tmpFormPage.getDescFontType()));
      newValues.add(toAuditTrailValue(tmpFormPage.getDescFontStyle()));
      newValues.add(toAuditTrailValue(tmpFormPage.getGroupID()));
      newValues.add(toAuditTrailValue(tmpFormPage.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpFormPage.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpFormPage.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpFormPage.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpFormPage.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpFormPage.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/

  public synchronized List getDetailListByFormRecordID(Integer frid) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.FORM_RECORD_ID, A.PARENT_PAGE_ID, A.TITLE, A.TITLE_ALT, A.PAGE_NO, A.PAGE_CODE, A.DESCRIPTION, A.DESCRIPTION_ALT, A.TITLE_FONT_COLOR, A.TITLE_FONT_SIZE, A.TITLE_FONT_TYPE, A.TITLE_FONT_STYLE, A.DESC_FONT_COLOR, A.DESC_FONT_SIZE, A.DESC_FONT_TYPE, A.DESC_FONT_STYLE, A.GROUP_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ,A.TITLE_POSITION ");
        sqlStat.append("FROM   FORM_PAGE A ");
        sqlStat.append("WHERE  A.FORM_RECORD_ID = ? AND A.RECORD_STATUS = ? ");
        sqlStat.append("ORDER BY A.PAGE_NO ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, frid);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          FormPage tmpFormPage = new FormPage();
          tmpFormPage.setID(getResultSetInteger(rs, "ID"));
          tmpFormPage.setFormRecordID(getResultSetInteger(rs, "FORM_RECORD_ID"));
          tmpFormPage.setParentPageID(getResultSetInteger(rs, "PARENT_PAGE_ID"));
          tmpFormPage.setTitle(getResultSetString(rs, "TITLE"));
          tmpFormPage.setTitleAlt(getResultSetString(rs, "TITLE_ALT"));
          tmpFormPage.setPageNo(getResultSetInteger(rs, "PAGE_NO"));
          tmpFormPage.setPageCode(getResultSetString(rs, "PAGE_CODE"));
          tmpFormPage.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpFormPage.setDescriptionAlt(getResultSetString(rs, "DESCRIPTION_ALT"));
          tmpFormPage.setTitleFontColor(getResultSetString(rs, "TITLE_FONT_COLOR"));
          tmpFormPage.setTitleFontSize(getResultSetInteger(rs, "TITLE_FONT_SIZE"));
          tmpFormPage.setTitleFontType(getResultSetString(rs, "TITLE_FONT_TYPE"));
          tmpFormPage.setTitleFontStyle(getResultSetString(rs, "TITLE_FONT_STYLE"));
          tmpFormPage.setDescFontColor(getResultSetString(rs, "DESC_FONT_COLOR"));
          tmpFormPage.setDescFontSize(getResultSetInteger(rs, "DESC_FONT_SIZE"));
          tmpFormPage.setDescFontType(getResultSetString(rs, "DESC_FONT_TYPE"));
          tmpFormPage.setDescFontStyle(getResultSetString(rs, "DESC_FONT_STYLE"));
          tmpFormPage.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
          tmpFormPage.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormPage.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormPage.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormPage.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormPage.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormPage.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormPage.settitlePosition(getResultSetString(rs, "TITLE_POSITION"));
          tmpFormPage.setCreatorName(UserInfoFactory.getUserFullName(tmpFormPage.getCreatorID()));
          tmpFormPage.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormPage.getUpdaterID()));
          result.add(tmpFormPage);
        }

        FormSectionDAObject formSectionDAO = new FormSectionDAObject(sessionContainer, dbConn);
        for (int i=0; i<result.size(); i++) {
          FormPage tmpFormPage = (FormPage) result.get(i);
          List formSectionList = formSectionDAO.getDetailListByFormPageID(tmpFormPage.getID());
          tmpFormPage.setFormSectionList(formSectionList);
          result.set(i, tmpFormPage);
        }

        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  public synchronized List getDetailListByFormRecordIDStepID(Integer formRecordID,Integer workflowStepID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    String permission = ""; //form page permission.
    String formRecordPermission =""; //form record permission.
    
    synchronized(dbConn) {
      try {
        MtmWfStepFormRecordDAObject mtmWfStepFormRecordDAO = new MtmWfStepFormRecordDAObject(sessionContainer, dbConn);
        FormSectionDAObject formSectionDAO = new FormSectionDAObject(sessionContainer, dbConn);
        
        if (workflowStepID.intValue()>0){
           formRecordPermission = mtmWfStepFormRecordDAO.getFormRecordPermissionByWorkflowStepIDFormRecordID(workflowStepID,formRecordID);
        }
        
        sqlStat.append("SELECT A.ID, A.FORM_RECORD_ID, A.PARENT_PAGE_ID, A.TITLE, A.TITLE_ALT, A.PAGE_NO, A.PAGE_CODE, A.DESCRIPTION, A.DESCRIPTION_ALT, A.TITLE_FONT_COLOR, A.TITLE_FONT_SIZE, A.TITLE_FONT_TYPE, A.TITLE_FONT_STYLE, A.DESC_FONT_COLOR, A.DESC_FONT_SIZE, A.DESC_FONT_TYPE, A.DESC_FONT_STYLE, A.GROUP_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ,A.TITLE_POSITION ");
        sqlStat.append("FROM   FORM_PAGE A ");
        sqlStat.append("WHERE  A.FORM_RECORD_ID = ? AND A.RECORD_STATUS = ? ");
        sqlStat.append("ORDER BY A.PAGE_NO ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, formRecordID);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          FormPage tmpFormPage = new FormPage();
          tmpFormPage.setID(getResultSetInteger(rs, "ID"));
          tmpFormPage.setFormRecordID(getResultSetInteger(rs, "FORM_RECORD_ID"));
          tmpFormPage.setFormRecordPermission(formRecordPermission);
          tmpFormPage.setParentPageID(getResultSetInteger(rs, "PARENT_PAGE_ID"));
          tmpFormPage.setTitle(getResultSetString(rs, "TITLE"));
          tmpFormPage.setTitleAlt(getResultSetString(rs, "TITLE_ALT"));
          tmpFormPage.setPageNo(getResultSetInteger(rs, "PAGE_NO"));
          tmpFormPage.setPageCode(getResultSetString(rs, "PAGE_CODE"));
          tmpFormPage.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpFormPage.setDescriptionAlt(getResultSetString(rs, "DESCRIPTION_ALT"));
          tmpFormPage.setTitleFontColor(getResultSetString(rs, "TITLE_FONT_COLOR"));
          tmpFormPage.setTitleFontSize(getResultSetInteger(rs, "TITLE_FONT_SIZE"));
          tmpFormPage.setTitleFontType(getResultSetString(rs, "TITLE_FONT_TYPE"));
          tmpFormPage.setTitleFontStyle(getResultSetString(rs, "TITLE_FONT_STYLE"));
          tmpFormPage.setDescFontColor(getResultSetString(rs, "DESC_FONT_COLOR"));
          tmpFormPage.setDescFontSize(getResultSetInteger(rs, "DESC_FONT_SIZE"));
          tmpFormPage.setDescFontType(getResultSetString(rs, "DESC_FONT_TYPE"));
          tmpFormPage.setDescFontStyle(getResultSetString(rs, "DESC_FONT_STYLE"));
          tmpFormPage.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
          tmpFormPage.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormPage.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormPage.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormPage.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormPage.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormPage.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormPage.settitlePosition(getResultSetString(rs, "TITLE_POSITION"));
          tmpFormPage.setCreatorName(UserInfoFactory.getUserFullName(tmpFormPage.getCreatorID()));
          tmpFormPage.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormPage.getUpdaterID()));
          if (workflowStepID.intValue()>0){
            permission = mtmWfStepFormRecordDAO.getFormPagePermissionByWorkflowStepIDFormRecordIDFormPageID(workflowStepID,formRecordID,tmpFormPage.getID());
          }
          tmpFormPage.setPermission(permission);
          result.add(tmpFormPage);
          
        }

        for (int i=0; i<result.size(); i++) {
          FormPage tmpFormPage = (FormPage) result.get(i);
          List formSectionList = formSectionDAO.getDetailListByFormRecordIDPageIDStepID(formRecordID,tmpFormPage.getID(),workflowStepID);
          tmpFormPage.setFormSectionList(formSectionList);
          result.set(i, tmpFormPage);
        }

        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  public synchronized List getDetailListByFormRecordIDDesc(Integer formRecordID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.FORM_RECORD_ID, A.PARENT_PAGE_ID, A.TITLE, A.TITLE_ALT, A.PAGE_NO, A.PAGE_CODE, A.DESCRIPTION, A.DESCRIPTION_ALT, A.TITLE_FONT_COLOR, A.TITLE_FONT_SIZE, A.TITLE_FONT_TYPE, A.TITLE_FONT_STYLE, A.DESC_FONT_COLOR, A.DESC_FONT_SIZE, A.DESC_FONT_TYPE, A.DESC_FONT_STYLE, A.GROUP_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ,A.TITLE_POSITION ");
        sqlStat.append("FROM   FORM_PAGE A ");
        sqlStat.append("WHERE  A.FORM_RECORD_ID = ? AND A.RECORD_STATUS = ? ");
        sqlStat.append("ORDER BY A.PAGE_NO DESC");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, formRecordID);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          FormPage tmpFormPage = new FormPage();
          tmpFormPage.setID(getResultSetInteger(rs, "ID"));
          tmpFormPage.setFormRecordID(getResultSetInteger(rs, "FORM_RECORD_ID"));
          tmpFormPage.setParentPageID(getResultSetInteger(rs, "PARENT_PAGE_ID"));
          tmpFormPage.setTitle(getResultSetString(rs, "TITLE"));
          tmpFormPage.setTitleAlt(getResultSetString(rs, "TITLE_ALT"));
          tmpFormPage.setPageNo(getResultSetInteger(rs, "PAGE_NO"));
          tmpFormPage.setPageCode(getResultSetString(rs, "PAGE_CODE"));
          tmpFormPage.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpFormPage.setDescriptionAlt(getResultSetString(rs, "DESCRIPTION_ALT"));
          tmpFormPage.setTitleFontColor(getResultSetString(rs, "TITLE_FONT_COLOR"));
          tmpFormPage.setTitleFontSize(getResultSetInteger(rs, "TITLE_FONT_SIZE"));
          tmpFormPage.setTitleFontType(getResultSetString(rs, "TITLE_FONT_TYPE"));
          tmpFormPage.setTitleFontStyle(getResultSetString(rs, "TITLE_FONT_STYLE"));
          tmpFormPage.setDescFontColor(getResultSetString(rs, "DESC_FONT_COLOR"));
          tmpFormPage.setDescFontSize(getResultSetInteger(rs, "DESC_FONT_SIZE"));
          tmpFormPage.setDescFontType(getResultSetString(rs, "DESC_FONT_TYPE"));
          tmpFormPage.setDescFontStyle(getResultSetString(rs, "DESC_FONT_STYLE"));
          tmpFormPage.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
          tmpFormPage.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormPage.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormPage.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormPage.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormPage.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormPage.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormPage.settitlePosition(getResultSetString(rs, "TITLE_POSITION"));
          tmpFormPage.setCreatorName(UserInfoFactory.getUserFullName(tmpFormPage.getCreatorID()));
          tmpFormPage.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormPage.getUpdaterID()));
          result.add(tmpFormPage);
        }

        FormSectionDAObject formSectionDAO = new FormSectionDAObject(sessionContainer, dbConn);
        for (int i=0; i<result.size(); i++) {
          FormPage tmpFormPage = (FormPage) result.get(i);
          List formSectionList = formSectionDAO.getDetailListByFormPageID(tmpFormPage.getID());
          tmpFormPage.setFormSectionList(formSectionList);
          result.set(i, tmpFormPage);
        }

        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized List getPageListByFormRecordID(Integer formRecordID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.FORM_RECORD_ID, A.PARENT_PAGE_ID, A.TITLE, A.TITLE_ALT, A.PAGE_NO, A.PAGE_CODE, A.DESCRIPTION, A.DESCRIPTION_ALT, A.TITLE_FONT_COLOR, A.TITLE_FONT_SIZE, A.TITLE_FONT_TYPE, A.TITLE_FONT_STYLE, A.DESC_FONT_COLOR, A.DESC_FONT_SIZE, A.DESC_FONT_TYPE, A.DESC_FONT_STYLE, A.GROUP_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE , A.TITLE_POSITION ");
        sqlStat.append("FROM   FORM_PAGE A ");
        sqlStat.append("WHERE  A.FORM_RECORD_ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, formRecordID);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          FormPage tmpFormPage = new FormPage();
          tmpFormPage.setID(getResultSetInteger(rs, "ID"));
          tmpFormPage.setFormRecordID(getResultSetInteger(rs, "FORM_RECORD_ID"));
          tmpFormPage.setParentPageID(getResultSetInteger(rs, "PARENT_PAGE_ID"));
          tmpFormPage.setTitle(getResultSetString(rs, "TITLE"));
          tmpFormPage.setTitleAlt(getResultSetString(rs, "TITLE_ALT"));
          tmpFormPage.setPageNo(getResultSetInteger(rs, "PAGE_NO"));
          tmpFormPage.setPageCode(getResultSetString(rs, "PAGE_CODE"));
          tmpFormPage.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpFormPage.setDescriptionAlt(getResultSetString(rs, "DESCRIPTION_ALT"));
          tmpFormPage.setTitleFontColor(getResultSetString(rs, "TITLE_FONT_COLOR"));
          tmpFormPage.setTitleFontSize(getResultSetInteger(rs, "TITLE_FONT_SIZE"));
          tmpFormPage.setTitleFontType(getResultSetString(rs, "TITLE_FONT_TYPE"));
          tmpFormPage.setTitleFontStyle(getResultSetString(rs, "TITLE_FONT_STYLE"));
          tmpFormPage.setDescFontColor(getResultSetString(rs, "DESC_FONT_COLOR"));
          tmpFormPage.setDescFontSize(getResultSetInteger(rs, "DESC_FONT_SIZE"));
          tmpFormPage.setDescFontType(getResultSetString(rs, "DESC_FONT_TYPE"));
          tmpFormPage.setDescFontStyle(getResultSetString(rs, "DESC_FONT_STYLE"));
          tmpFormPage.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
          tmpFormPage.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormPage.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormPage.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormPage.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormPage.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormPage.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormPage.settitlePosition(getResultSetString(rs, "TITLE_POSITION"));
          tmpFormPage.setCreatorName(UserInfoFactory.getUserFullName(tmpFormPage.getCreatorID()));
          tmpFormPage.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormPage.getUpdaterID()));

          result.add(tmpFormPage);
        }
        return result;
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * get the permission in the page.
   * @param formRecordID
   * @param formPageID
   * @param workflowStepID
   * @return
   * @throws ApplicationException
   */
  public synchronized boolean getFormPagePermissionByRecordIDPageIDStepID(Integer formRecordID, Integer formPageID, Integer workflowStepID,boolean isViewUserStep) throws ApplicationException {
    String permission = ""; //form page permission.
    boolean hasPermission = false;
    
    synchronized(dbConn) {
      try {
        MtmWfStepFormRecordDAObject mtmWfStepFormRecordDAO = new MtmWfStepFormRecordDAObject(sessionContainer, dbConn);
        FormSectionDAObject formSectionDAO = new FormSectionDAObject(sessionContainer, dbConn);

          if (workflowStepID.intValue()>0){
            permission = mtmWfStepFormRecordDAO.getFormPagePermissionByWorkflowStepIDFormRecordIDFormPageID(workflowStepID,formRecordID,formPageID);
            if (!Utility.isEmpty(permission) && !"null".equals(permission)){
              if (!isViewUserStep){
                hasPermission = true; 
              }else if (isViewUserStep && !WorkflowStep.FORM_PROCESS_PERMISSION_READ.equals(permission)){
                hasPermission = true; 
              }             
            }else{
                hasPermission = formSectionDAO.getPageSectionsPermissionByFormRecordIDPageIDStepID(formRecordID,formPageID,workflowStepID,isViewUserStep);         
              }
          }else{
            hasPermission = true; 
          }
                   
        return(hasPermission);
      } catch (ApplicationException appEx) {
        throw appEx;      
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } 
    }
  }
/**
 * set the Form Element Mandatory From WorkFlow Binding 
 * @param formPageList
 * @param formRecordID
 * @param workflowStepID
 */
  public void setMandatoryFromWFStep(List formPageList) {
    for (Iterator iter = formPageList.iterator(); iter.hasNext();) {
      FormPage formPage = (FormPage) iter.next();
      List sectionList = formPage.getFormSectionList();
      for (Iterator iterator = sectionList.iterator(); iterator.hasNext();) {
        FormSection section = (FormSection) iterator.next();
        List elementList = section.getFormElementList();
        if ("M".endsWith(section.getPermission())) {
          for (Iterator sectionIterator = elementList.iterator(); sectionIterator.hasNext();) {
            FormElement element = (FormElement) sectionIterator.next();
            element.setRequiredFlag(GlobalConstant.TRUE);

          }
        }
      }
    }

  }
}
